package com.mm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mm.entity.ActiveCodeEntity;
import com.mm.entity.Result;
import com.mm.mapper.ActiveCodeMapper;
import com.mm.service.SendMailService;
import com.mm.utils.DateUtil;
import com.mm.utils.RandomDataUtil;
import com.mm.utils.ResultEnum;
import com.mm.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 邮件发送接口
 */
@RestController
@RequestMapping("/send")
public class UserEmailController {

    @Autowired
    SendMailService sendMailService;
    @Autowired
    ActiveCodeMapper activeCodeMapper;

    DateUtil dateUtil = new DateUtil();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    /**
     * 注册邮箱发送接口
     *
     * @param email 邮箱地址
     * @return
     */
    @RequestMapping(value = "/logonVerify", method = RequestMethod.POST)
    public Result logonVerify(@RequestParam String email) {
        RandomDataUtil randomDataUtil = new RandomDataUtil();
        String code = randomDataUtil.creatCode(5);
        if (sendMailService.sendMailToText(email, "欢迎加入", "您的验证码为【" + code + "】")) {
            ActiveCodeEntity activeCodeEntity = new ActiveCodeEntity();
            activeCodeEntity.setEmail(email);
            activeCodeEntity.setCode(code);
            activeCodeEntity.setActiveLength(1);
            activeCodeEntity.setCreateTime(dateFormat.format(new Date()));
            activeCodeMapper.insert(activeCodeEntity);
            return ResultUtil.success() ;
        }
        return ResultUtil.error(ResultEnum.EMAIL_SEND_ERROR.getCode(), ResultEnum.EMAIL_SEND_ERROR.getMsg()) ;
    }

    /**
     * 注册邮箱验证接口
     *
     * @return
     */
    @RequestMapping(value = "/verifyCode", method = RequestMethod.POST)
    public Result verifyCode(@RequestParam String email, @RequestParam String code) {
        // 查询验证码
        QueryWrapper<ActiveCodeEntity> sectionQueryWrapper = new QueryWrapper<>();
        sectionQueryWrapper.eq("email", email);
        sectionQueryWrapper.eq("code", code);
        ActiveCodeEntity activeCodeEntity = activeCodeMapper.selectOne(sectionQueryWrapper);
        if (activeCodeEntity != null){
            // 判断是否过期
            try {
                long difference = dateUtil.dateDifference(activeCodeEntity.getCreateTime(), dateFormat.format(new Date()));
                if (difference < activeCodeEntity.getActiveLength()){
                    activeCodeMapper.deleteById(activeCodeEntity.getId()) ;
                    return ResultUtil.success() ;
                }else {
                    activeCodeMapper.deleteById(activeCodeEntity.getId()) ;
                    return ResultUtil.error(ResultEnum.CODE_OVERDUE_ERROR.getCode(), ResultEnum.CODE_OVERDUE_ERROR.getMsg()) ;
                }
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
        return ResultUtil.error(ResultEnum.CODE_INFO_ERROR.getCode(), ResultEnum.CODE_INFO_ERROR.getMsg()) ;

    }
}
